<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Document,  Opportunity } from '@element-plus/icons-vue'
import { getOverviewApi } from '@/api/home'

const router = useRouter()

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0
})

// 线索数据配置
const clueData = [
  { key: 'clueTotal', label: '总线索数', icon: 'Collection' },
  { key: 'clueWaitAllot', label: '待分配线索数', icon: 'Timer' },
  { key: 'clueWaitFollow', label: '待跟进线索数', icon: 'List' },
  { key: 'clueFollowing', label: '跟进中线索数', icon: 'Check' },
  { key: 'clueFalse', label: '伪线索数', icon: 'Close' },
  { key: 'clueConvertBusiness', label: '转商机线索数', icon: 'Star' }
]

// 商机数据配置
const businessData = [
  { key: 'businessTotal', label: '总商机数', icon: 'Collection' },
  { key: 'businessWaitAllot', label: '待分配商机数', icon: 'Timer' },
  { key: 'businessWaitFollow', label: '待跟进商机数', icon: 'List' },
  { key: 'businessFollowing', label: '跟进中商机数', icon: 'Check' },
  { key: 'businessFalse', label: '回收商机数', icon: 'Close' },
  { key: 'businessConvertCustomer', label: '转客户商机数', icon: 'Star' }
]

// 快捷操作配置
const quickActions = [
  {
    title: '课程管理',
    subtitle: '课程信息维护',
    icon: 'Reading',
    path: '/resource/course'
  },
  {
    title: '活动管理',
    subtitle: '营销活动管理',
    icon: 'Calendar',
    path: '/resource/activity'
  },
  {
    title: '线索管理',
    subtitle: '潜在客户追踪',
    icon: 'Document',
    path: '/clue/list'
  },
  {
    title: '商机管理',
    subtitle: '销售机会管理',
    icon: 'Opportunity',
    path: '/business/list'
  },
  {
    title: '客户管理',
    subtitle: '客户信息维护',
    icon: 'User',
    path: '/customer'
  },
  {
    title: '日志统计',
    subtitle: '系统操作日志',
    icon: 'DocumentCopy',
    path: '/system/log'
  }
]

// 获取概览数据
const fetchOverviewData = async () => {
  try {
    const response = await getOverviewApi()
    if (response.code === 1) {
      overviewData.value = response.data
    } else {
      ElMessage.error(response.msg || '获取数据失败')
    }
  } catch (error) {
    console.error('获取概览数据失败:', error)
    ElMessage.error('获取数据失败，请稍后重试')
  }
}

// 页面跳转
const navigateTo = (path) => {
  router.push(path)
}

// 组件挂载时获取数据
onMounted(() => {
  fetchOverviewData()
})
</script>

<template>
  <div class="home-container">
    <!-- 欢迎标题 -->
    <div class="welcome-section">
      <div class="welcome-card">
        <h1 class="welcome-title">欢迎您，进入轻客管家系统</h1>
        <p class="welcome-subtitle">智能化的客户关系管理平台</p>
      </div>
    </div>

    <!-- 概览数据区域 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-card clue-card">
        <div class="card-header">
          <div class="card-icon">
            <el-icon><Document /></el-icon>
          </div>
          <h3 class="card-title">线索概览</h3>
        </div>
        <div class="data-grid">
          <div class="data-item" v-for="(item, index) in clueData" :key="index">
            <div class="data-icon">
              <el-icon><component :is="item.icon" /></el-icon>
            </div>
            <div class="data-content">
              <div class="data-label">{{ item.label }}</div>
              <div class="data-value">{{ overviewData[item.key] || 0 }}</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 商机概览 -->
      <div class="overview-card business-card">
        <div class="card-header">
          <div class="card-icon">
            <el-icon><Opportunity /></el-icon>
          </div>
          <h3 class="card-title">商机概览</h3>
        </div>
        <div class="data-grid">
          <div class="data-item" v-for="(item, index) in businessData" :key="index">
            <div class="data-icon">
              <el-icon><component :is="item.icon" /></el-icon>
            </div>
            <div class="data-content">
              <div class="data-label">{{ item.label }}</div>
              <div class="data-value">{{ overviewData[item.key] || 0 }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions-section">
      <h3 class="section-title">快捷操作</h3>
      <div class="action-grid">
        <div class="action-item" v-for="(action, index) in quickActions" :key="index" @click="navigateTo(action.path)">
          <div class="action-icon">
            <el-icon><component :is="action.icon" /></el-icon>
          </div>
          <div class="action-title">{{ action.title }}</div>
          <div class="action-subtitle">{{ action.subtitle }}</div>
          <div class="action-hover-effect"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  padding: 30px;
  background: #ffffff;
  min-height: calc(100vh - 100px);
  position: relative;
}

.home-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="75" cy="75" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="50" cy="10" r="0.5" fill="rgba(255,255,255,0.1)"/><circle cx="10" cy="60" r="0.5" fill="rgba(255,255,255,0.1)"/><circle cx="90" cy="40" r="0.5" fill="rgba(255,255,255,0.1)"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  opacity: 0.3;
  pointer-events: none;
}

.welcome-section {
  text-align: center;
  margin-bottom: 40px;
  position: relative;
  z-index: 1;
}

.welcome-card {
  background: linear-gradient(135deg, #69b3f3 0%, #20d3e0fd 100%);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(151, 197, 240, 0.2);
  border: 1px solid rgba(24, 144, 255, 0.3);
  transform: translateY(0);
  transition: all 0.3s ease;
}

.welcome-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.15);
}

.welcome-title {
  font-size: 32px;
  color: #ffffff;
  margin: 0 0 15px 0;
  font-weight: 600;
}

.welcome-subtitle {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.9);
  margin: 0;
  font-weight: 400;
}

.overview-section {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
  position: relative;
  z-index: 1;
}

.overview-card {
  flex: 1;
  background: #ffffff;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 15px 30px rgba(24, 144, 255, 0.15);
  border: 2px solid #e6f7ff;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.overview-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #1890ff, #096dd9);
}

.clue-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.15);
}

.business-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.15);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  gap: 12px;
}

.card-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1890ff, #096dd9);
  color: white;
  font-size: 20px;
}

.card-title {
  font-size: 18px;
  color: #2c3e50;
  margin: 0;
  font-weight: 600;
}

.data-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}

.data-item {
  background: linear-gradient(135deg, #f0f8ff, #e6f7ff);
  border-radius: 12px;
  padding: 15px;
  text-align: center;
  border: 1px solid #bae7ff;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.data-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 25px rgba(24, 144, 255, 0.2);
  background: linear-gradient(135deg, #e6f7ff, #bae7ff);
  border-color: #1890ff;
}

.data-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(24, 144, 255, 0.1), rgba(9, 109, 217, 0.1));
  opacity: 0;
  transition: opacity 0.3s ease;
}

.data-item:hover::before {
  opacity: 1;
}

.data-icon {
  margin-bottom: 12px;
  color: #1890ff;
  font-size: 20px;
}

.data-content {
  position: relative;
  z-index: 1;
}

.data-label {
  font-size: 11px;
  color: #5a6c7d;
  margin-bottom: 8px;
  line-height: 1.3;
  font-weight: 500;
}

.data-value {
  font-size: 24px;
  font-weight: 700;
  background: linear-gradient(135deg, #1890ff, #096dd9);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.quick-actions-section {
  position: relative;
  z-index: 1;
  margin-top: 15px;
}

.section-title {
  font-size: 22px;
  color: #1890ff;
  margin: 0 0 25px 0;
  font-weight: 600;
  text-align: center;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 20px;
}

.action-item {
  background: #ffffff;
  border-radius: 16px;
  padding: 25px 15px;
  text-align: center;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  border: 2px solid #e6f7ff;
  box-shadow: 0 12px 25px rgba(24, 144, 255, 0.1);
  position: relative;
  overflow: hidden;
}

.action-item:hover {
  transform: translateY(-10px) scale(1.02);
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.2);
  border-color: rgba(24, 144, 255, 0.5);
}

.action-hover-effect {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
  transition: left 0.5s ease;
}

.action-item:hover .action-hover-effect {
  left: 100%;
}

.action-icon {
  margin-bottom: 15px;
  transition: all 0.3s ease;
}

.action-icon .el-icon {
  font-size: 32px;
  background: linear-gradient(135deg, #1890ff, #096dd9);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.action-item:hover .action-icon .el-icon {
  transform: scale(1.1);
}

.action-title {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 8px;
  transition: color 0.3s ease;
}

.action-item:hover .action-title {
  color: #1890ff;
}

.action-subtitle {
  font-size: 12px;
  color: #7f8c8d;
  line-height: 1.3;
  transition: color 0.3s ease;
}

.action-item:hover .action-subtitle {
  color: #5a6c7d;
}

/* 响应式设计 */
@media (max-width: 1400px) {
  .action-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 1200px) {
  .overview-section {
    flex-direction: column;
  }
  
  .action-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .home-container {
    padding: 20px;
  }
  
  .welcome-card {
    padding: 30px 20px;
  }
  
  .welcome-title {
    font-size: 24px;
  }
  
  .action-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .data-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .overview-card {
    padding: 20px;
  }
}

@media (max-width: 480px) {
  .action-grid {
    grid-template-columns: 1fr;
  }
  
  .data-grid {
    grid-template-columns: 1fr;
  }
}

/* 添加一些微妙的动画 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.welcome-section,
.overview-section,
.quick-actions-section {
  animation: fadeInUp 0.6s ease-out;
}

.overview-section {
  animation-delay: 0.2s;
}

.quick-actions-section {
  animation-delay: 0.4s;
}

/* 数据项的进入动画 */
.data-item {
  animation: fadeInUp 0.6s ease-out;
}

.data-item:nth-child(1) { animation-delay: 0.1s; }
.data-item:nth-child(2) { animation-delay: 0.2s; }
.data-item:nth-child(3) { animation-delay: 0.3s; }
.data-item:nth-child(4) { animation-delay: 0.4s; }
.data-item:nth-child(5) { animation-delay: 0.5s; }
.data-item:nth-child(6) { animation-delay: 0.6s; }

/* 操作项的进入动画 */
.action-item {
  animation: fadeInUp 0.6s ease-out;
}

.action-item:nth-child(1) { animation-delay: 0.5s; }
.action-item:nth-child(2) { animation-delay: 0.6s; }
.action-item:nth-child(3) { animation-delay: 0.7s; }
.action-item:nth-child(4) { animation-delay: 0.8s; }
.action-item:nth-child(5) { animation-delay: 0.9s; }
.action-item:nth-child(6) { animation-delay: 1.0s; }
</style>
